<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JS 数值计算练习</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: "Microsoft Yahei", sans-serif;
    }
    body {
      background-color: #f5f7fa;
      padding: 2rem;
    }
    .container {
      max-width: 800px;
      margin: 0 auto;
      background-color: #fff;
      padding: 2rem;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }
    h1 {
      color: #333;
      margin-bottom: 1.5rem;
      text-align: center;
    }
    .numbers-list {
      margin-bottom: 2rem;
      padding: 1rem;
      background-color: #f0f2f5;
      border-radius: 4px;
      overflow-x: auto;
    }
    .numbers-list p {
      white-space: nowrap;
      line-height: 1.8;
    }
    .btn {
      background-color: #007bff;
      color: #fff;
      border: none;
      padding: 0.8rem 1.5rem;
      border-radius: 4px;
      cursor: pointer;
      font-size: 1rem;
      transition: background-color 0.3s;
    }
    .btn:hover {
      background-color: #0056b3;
    }
    .results {
      margin-top: 2rem;
    }
    .result-item {
      margin-bottom: 1rem;
      padding: 1rem;
      background-color: #e9ecef;
      border-radius: 4px;
    }
    .result-item h3 {
      margin-bottom: 0.5rem;
      color: #212529;
    }
    .result-item p {
      font-size: 1.1rem;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>数值计算练习</h1>
    <div class="numbers-list">
      <p>
        2.481 2.484 2.462 2.466 2.458 <br>
        2.467 2.453 2.457 2.457 2.462 <br>
        2.496 2.500 2.470 2.462 2.459 <br>
        2.473 2.466 2.469 2.483 2.480 <br>
        2.509 2.483 2.466 2.472 2.490
      </p >
    </div>
    <button class="btn" id="calculateBtn">计算统计结果</button>
    <div class="results" id="resultsContainer"></div>
  </div>

  <script>
    const numbers = [
      2.481, 2.484, 2.462, 2.466, 2.458,
      2.467, 2.453, 2.457, 2.457, 2.462,
      2.496, 2.500, 2.470, 2.462, 2.459,
      2.473, 2.466, 2.469, 2.483, 2.480,
      2.509, 2.483, 2.466, 2.472, 2.490
    ];

    const calculateBtn = document.getElementById('calculateBtn');
    const resultsContainer = document.getElementById('resultsContainer');

    calculateBtn.addEventListener('click', () => {
      // 计算平均值
      let sum = 0;
      for (let num of numbers) {
        sum += num;
      }
      const average = sum / numbers.length;
      const formattedAverage = average.toFixed(3);

      // 计算最大偏差
      let maxDeviation = 0;
      for (let num of numbers) {
        const deviation = Math.abs(num - average);
        if (deviation > maxDeviation) {
          maxDeviation = deviation;
        }
      }

      // 计算最大值
      const max = Math.max(...numbers);
      const formattedMax = max.toFixed(3);

      // 计算最小值
      const min = Math.min(...numbers);
      const formattedMin = min.toFixed(3);

      // 渲染结果
      resultsContainer.innerHTML = `
        <div class="result-item">
          <h3>平均值（精确到小数点后三位）</h3>
          <p>${formattedAverage}</p >
        </div>
        <div class="result-item">
          <h3>单个值与平均值的最大偏差</h3>
          <p>${maxDeviation.toFixed(6)}</p >
        </div>
        <div class="result-item">
          <h3>最大值（保留三位小数）</h3>
          <p>${formattedMax}</p >
        </div>
        <div class="result-item">
          <h3>最小值（保留三位小数）</h3>
          <p>${formattedMin}</p >
        </div>
      `;
    });
  </script>
</body>
</html>